<?php

define( 'data_path', rootpath . 'grabber/data/');

//include_once( rootpath . 'Smarty.class.php');
//$main_smarty = new Smarty;

/*include( rootpath . 'config.php');
include(mnminclude.'html1.php');
include(mnminclude.'link.php');
include(mnminclude.'tags.php');
include(mnminclude.'search.php');
include(mnminclude.'smartyvariables.php');*/


require_once rootpath .'grabber/dbsimple/config.php';
require_once rootpath .'grabber/dbsimple/Connect.php';

$dbConfigs = array(
	'driver' => 'mysql',
	'user' => 'test',
	'password' => 'pass',
	'host' => 'localhost',
	'db' => 'dbname',
	'charset' => 'UTF-8',
);
$dsn =	$dbConfigs['driver'] .'://'. 
				$dbConfigs['user'] .':'. $dbConfigs['password'] .'@'. 
				$dbConfigs['host'] .'/'. $dbConfigs['db'] .'?charset='. $dbConfigs['charset'];

$db = new \DbSimple_Connect($dsn);

require_once( rootpath . 'grabber/core/translate/GTranslate.php');

require_once( rootpath . 'grabber/core/Proxy.php');
require_once( rootpath . 'grabber/core/Browser.php');
require_once( rootpath . 'grabber/core/model/Abstract.php');

require_once( rootpath . 'grabber/core/model/Parser.php');
require_once( rootpath . 'grabber/core/model/Parser/News.php');
require_once( rootpath . 'grabber/core/model/Parser/Comment.php');

require_once rootpath . 'grabber/grabbers/abstract.php';

require_once( rootpath . 'grabber/core/model/users.php');
require_once( rootpath . 'grabber/core/model/links.php');
require_once( rootpath . 'grabber/core/model/comments.php');
require_once( rootpath . 'grabber/core/model/file.php');


class Grabber_Application
{
	
	protected $_db = null;


	protected $_oProxy = null;
	protected $_oBrowser = null;
	protected $_mNews = null;
	protected $_mParser = null;
	protected $_mComment = null;
	
	protected $_oParser = null;


	public function __construct( $db )
	{
		$this->_db = $db;
		
		$this->_oProxy   = new Proxy();
		$this->_oBrowser = new Browser();

		$this->_mNews	  = new Parser_News( $this->_db );
		$this->_mParser  = new Parser( $this->_db );
		$this->_mComment = new Parser_Comment( $this->_db );
	}
	
	public function run( $_request )
	{
		$this->_callOperation( $_request );
	}
	
	protected function _callOperation( $_request )
	{
		if( !empty($_request['install']) && $_request['install'] == 1 && $_request['user'] == 'admin' && $_request['pass'] == 'nhfnfnf' )
		{
			require_once rootpath . 'grabber/install.php';
			return true;
		}
		
		$parser_row = array();
		if(!empty($_request['parser']))
		{
			$parser_row = $_request['parser'];
		}
		else
		{
			$parser_row = $this->_mParser->getParserForUpdate( date('Y-m-d', time()) );
		}
		$this->_oParser = $this->_loadParser( $parser_row );
		
		return $this->_callFunc( $this->_oParser, $_request );
		
		if( $this->_callFunc( $this->_oParser, $_request ) )
			return true;

		$parser_row = $this->_mParser->getParserForUpdate( );
		$this->_oParser = $this->_loadParser( $parser_row );
		
		if( $this->_callFunc( $this->_oParser, $_request ) )
			return true;
	}
	
	protected function _callFunc( $obj, $_request )
	{
		if( !$obj || empty( $_request['operation'] ) )
			return false;
		
		$method = $_request['operation'];
		$method[0] = mb_strtoupper( $method[0] );
		$method = 'parsing' . $method;
		
		if( method_exists( $obj , $method ) )
		{
			return call_user_method( $method, $obj );
		}
		
		return false;
	}

	protected function _loadParser( $parser_row )
	{
		if( !empty( $parser_row ) && $parser_row->parser_name )
		{
			$class_name = str_replace('.', '_', $parser_row->parser_name);
			if( !empty( $class_name[0] ) )
			{
				$class_name[0] = strtoupper( $class_name[0] );
			}

			require_once rootpath . 'grabber/grabbers/'. $parser_row->parser_name .'.php';

			return new $class_name( $this->_db, $this->_mParser, $this->_mNews, $this->_mComment, $this->_oBrowser, $this->_oProxy );
		}
		
		return null;
	}
	
}
